#include <string>
#include "atomic.h"
#include "message.h"
#include "mainsimu.h"
#include "strutil.h"
#include "../HeadFile/PCS_CDU_processing_1_model.h"


/**** the Constructor ****/
PCS_CDU_processing_1_model::PCS_CDU_processing_1_model( const string &name)
: Atomic( name )
, PCS_CDU_1_stream_crude( this->addInputPort( "PCS_CDU_1_stream_crude") )
, PCS_CDU_1_stream_mode( this->addInputPort( "PCS_CDU_1_stream_mode") )
, PCS_CDU_1_stream_NA( this->addOutputPort( "PCS_CDU_1_stream_NA") )
, PCS_CDU_1_stream_LD( this->addOutputPort( "PCS_CDU_1_stream_LD") )
, PCS_CDU_1_stream_OP( this->addOutputPort( "PCS_CDU_1_stream_OP") )
, PCS_CDU_1_stream_VR( this->addOutputPort( "PCS_CDU_1_stream_VR") )
, PCS_CDU_1_stream_VGO( this->addOutputPort( "PCS_CDU_1_stream_VGO") )
, PCS_CDU_1_stream_KER( this->addOutputPort( "PCS_CDU_1_stream_KER") )
{

		
//	string tmpval1(MainSimulator::Instance().getParameter( this->description(), "PCS_CDU_1_stream_VGO_yield_val" ));
//	string tmpval2(MainSimulator::Instance().getParameter( this->description(), "PCS_CDU_1_stream_LD_yield_val" ));
//	string tmpval3(MainSimulator::Instance().getParameter( this->description(), "PCS_CDU_1_stream_NA_yield_val" ));
//	string tmpval4(MainSimulator::Instance().getParameter( this->description(), "PCS_CDU_1_stream_crude_yield_val" ));
//	string tmpval5(MainSimulator::Instance().getParameter( this->description(), "PCS_CDU_1_stream_KER_yield_val" ));
//	string tmpval6(MainSimulator::Instance().getParameter( this->description(), "PCS_CDU_1_stream_VR_yield_val" ));
//	PCS_CDU_1_stream_VGO_yield_val = str2float(tmpval1);
//	PCS_CDU_1_stream_LD_yield_val = str2float(tmpval2);
//	PCS_CDU_1_stream_NA_yield_val = str2float(tmpval3);
//	PCS_CDU_1_stream_crude_yield_val = str2float(tmpval4);
//	PCS_CDU_1_stream_KER_yield_val = str2float(tmpval5);
//	PCS_CDU_1_stream_VR_yield_val = str2float(tmpval6);
		
		
}


/*** the initFunction ***/
Model &PCS_CDU_processing_1_model::initFunction()
{

		
	count = 0;
	delay = "00:00:00:00";
	passivate();
	return *this;
		
		
}

/*** the externalFunction ***/
Model &PCS_CDU_processing_1_model::externalFunction( const ExternalMessage &msg )
{

			
	if(msg.port() == PCS_CDU_1_stream_mode)
	{
		PCS_CDU_1_stream_mode_val = msg.value();
		if(PCS_CDU_1_stream_mode_val == 0)
		{
			PCS_CDU_1_stream_crude_yield_val = 1.0;
			PCS_CDU_1_stream_NA_yield_val = 0.04;
			PCS_CDU_1_stream_KER_yield_val = 0.14;
			PCS_CDU_1_stream_LD_yield_val = 0.23;
			PCS_CDU_1_stream_VGO_yield_val = 0.21;
			PCS_CDU_1_stream_VR_yield_val = 0.38;
		}
		else
		{
			PCS_CDU_1_stream_crude_yield_val = 1.0;
			PCS_CDU_1_stream_NA_yield_val = 0.05;
			PCS_CDU_1_stream_KER_yield_val = 0.15;
			PCS_CDU_1_stream_LD_yield_val = 0.18;
			PCS_CDU_1_stream_VGO_yield_val = 0.22;
			PCS_CDU_1_stream_VR_yield_val = 0.4;
		}
	}

	if( msg.port() == PCS_CDU_1_stream_crude ){
		PCS_CDU_1_stream_crude_flowrate = msg.value();
		count = 1;
		holdIn( active, delay);
	}
	return *this;
		
		
}

/*** the internalFunction ***/
Model &PCS_CDU_processing_1_model::internalFunction( const InternalMessage &msg )
{

			
	passivate();
	return *this;
		
		
}

/*** the outputFunction ***/
Model &PCS_CDU_processing_1_model::outputFunction( const InternalMessage &msg )
{

			
	if(count == 1)
	{
		count = 0;
		PCS_CDU_1_stream_NA_flowrate = PCS_CDU_1_stream_crude_flowrate * PCS_CDU_1_stream_NA_yield_val;
		PCS_CDU_1_stream_KER_flowrate = PCS_CDU_1_stream_crude_flowrate * PCS_CDU_1_stream_KER_yield_val;
		PCS_CDU_1_stream_LD_flowrate = PCS_CDU_1_stream_crude_flowrate * PCS_CDU_1_stream_LD_yield_val;
		PCS_CDU_1_stream_VGO_flowrate = PCS_CDU_1_stream_crude_flowrate * PCS_CDU_1_stream_VGO_yield_val;
		PCS_CDU_1_stream_VR_flowrate = PCS_CDU_1_stream_crude_flowrate * PCS_CDU_1_stream_VR_yield_val;
		sendOutput( msg.time(), PCS_CDU_1_stream_OP, PCS_CDU_1_stream_crude_flowrate);
		sendOutput( msg.time(), PCS_CDU_1_stream_NA, PCS_CDU_1_stream_NA_flowrate);
		sendOutput( msg.time(), PCS_CDU_1_stream_KER, PCS_CDU_1_stream_KER_flowrate);
		sendOutput( msg.time(), PCS_CDU_1_stream_LD, PCS_CDU_1_stream_LD_flowrate);
		sendOutput( msg.time(), PCS_CDU_1_stream_VGO, PCS_CDU_1_stream_VGO_flowrate);
		sendOutput( msg.time(), PCS_CDU_1_stream_VR, PCS_CDU_1_stream_VR_flowrate);
	}
	return *this;
		
		
}

